package com.jzg.common.util;

/*
*java中对日期的加减操作
*gc.add(1,-1)表示年份减一.
*gc.add(2,-1)表示月份减一.
*gc.add(3.-1)表示周减一.
*gc.add(5,-1)表示天减一.
*以此类推应该可以精确的毫秒吧.没有再试.大家可以试试.
*GregorianCalendar类的add(int field,int amount)方法表示年月日加减.
*field参数表示年,月.日等.
*amount参数表示要加减的数量.
*
* DateUtil.java 测试如下:
*/

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.jfinal.ext.kit.DateKit;
import com.jfinal.kit.StrKit;
import com.jzg.common.model.biz.Holiday;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateUtil {

	
	public static String calFullDate(Date date, int yearNum, int monthNum, int dateNum,int hourNum) {
        String result = "";
        try {
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.YEAR, yearNum);//年
            cal.add(Calendar.MONTH, monthNum);//月
            cal.add(Calendar.DATE, dateNum);//日
            cal.add(Calendar.HOUR, hourNum);//时
            cal.add(Calendar.MINUTE, hourNum);//分
            cal.add(Calendar.SECOND, hourNum);//秒
            result = sd.format(cal.getTime());
        } catch (Exception e) {
        }
        return result;
    }
	

	public static String getYears(Date d) {
		GregorianCalendar gc = new GregorianCalendar();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		gc.setTime(d);
		gc.add(1, +1);
		gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DATE));

		return sf.format(gc.getTime());
	}
	
	public static long getDayForTime(String dateStr,String format) throws ParseException {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		Date oneDate  = dateFormat.parse(dateStr);
		long days = Math.abs(((oneDate.getTime()-date.getTime())/(1000*60*60*24)));
		
		return days;
	}
	

	public String getHalfYear(Date d) {
		GregorianCalendar gc = new GregorianCalendar();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		gc.setTime(d);
		gc.add(2, +6);
		gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DATE));

		return sf.format(gc.getTime());
	}

	public String getQuarters(Date d) {
		GregorianCalendar gc = new GregorianCalendar();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		gc.setTime(d);
		gc.add(2, +3);
		gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), gc.get(Calendar.DATE));
		return sf.format(gc.getTime());
	}

	public String getLocalDate(Date d) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		return sf.format(d);
	}
	
	/**
	 * 返回指定日期往前或往后n天日期
	 * @param dateStr
	 * @param days
	 * @return
	 */
	public static String getFrontday(String dateStr,int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(DateKit.toDate(dateStr));
		calendar.add(Calendar.DAY_OF_MONTH,days);
		Date twoDay = calendar.getTime();
		return DateKit.toStr(twoDay, "yyyy-MM-dd");
	}
	
	/**
	 * 获取指定日志星期几
	 * @param dateStr
	 * @return
	 */
	public static int getDateToWeek(String dateStr) {
		Calendar cal = Calendar.getInstance();
		Date date = DateKit.toDate(dateStr);
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_WEEK)-1;
	}
	
	/**
	 * 指定日期按节假日和周末顺延至上一个工作日
	 * @param dateStr 日期字符串
	 * @param type 0-拘留，1-批捕、取保、监居（周五不报诉）
	 * @return
	 */
	public static String getrRealDate(String dateStr,boolean type) {
		String returnStr="";int i=0;
		if(type) {
			i=-1;
		}
		List<Holiday> holidays = Holiday.dao.findAll();
		if(holidays!=null && holidays.size()>0) {
			for (Holiday holiday : holidays) {
				if(holiday.getDays().contains(dateStr)) {
					if(!StrKit.isBlank(holiday.getStartDay())) {
						returnStr = getFrontday(holiday.getStartDay(),-1);
						if(type && DateUtil.getDateToWeek(returnStr)==5) {
							returnStr = getFrontday(holiday.getStartDay(),-1);
						}
					}
					break;
				}
			}
		}
		if(StrKit.isBlank(returnStr)) {
			int week = DateUtil.getDateToWeek(dateStr);
			if(week==0) {
				returnStr = getFrontday(dateStr,-2+i);
			} else if (week==6) {
				returnStr = getFrontday(dateStr,-1+i);
			}else {
				returnStr = dateStr;
				if(week==5) {
					returnStr = getFrontday(dateStr,-1);
				}
			}
		}
		return returnStr;
	}
	
	/**
	 * 获取指定日期与当前日期相差天数
	 * @param dateStr
	 * @return
	 */
	public static long getDifferenceForDay(String dateStr) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = dateFormat.parse(dateFormat.format(new Date()));
			Date dateOne = dateFormat.parse(dateStr);
			long days = ((dateOne.getTime()-date.getTime())/(1000*60*60*24));
			return days;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	public static boolean dateCompare(String date1,String date2) throws ParseException{
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date sd1=df.parse(date1);
		Date sd2=df.parse(date2);
		return sd1.before(sd2);
	}
	
	public static void main(String[] args) throws ParseException {
	}

}